package com.heima.media.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.service.IWmChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(tags = "频道管理API") // knife4j的
public class WmChannelController {

    @Autowired
    IWmChannelService channelService;

    /**
     * 根据名称分页查询频道列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "根据名称分页查询频道列表",notes = "author:pg")
    @ApiImplicitParam(name = "dto",value = "查询对象",required = true,dataType = "ChannelDto")
    public ResponseResult listByName(@RequestBody ChannelDto dto){
        // 根据名称模糊查询 加分页
        return channelService.listByName(dto);
    }

    /**
     * 新增频道
     * @param channel
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增频道",notes = "author:pg")
    @ApiImplicitParam(name = "channel",value = "频道对象",required = true,dataType = "WmChannel")
    public ResponseResult<Integer> saveChannel(@RequestBody WmChannel channel){
        // 新增频道的方法
        return channelService.saveChannel(channel);
    }

    /**
     * 频道列表的展示, 查询所有频道
     */
    @GetMapping("/channels")
    public ResponseResult channels(){
        //1. 查询所有频道 构建查询条件
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        //2. 设置排序, 根据名称降序排序
        query.orderByDesc(WmChannel::getName);
        //3. plus 进行列表查询
        List<WmChannel> list = channelService.list(query);
        //4. 简单业务, 直接返回给前端
        return ResponseResult.okResult(list);
    }

}
